package audio;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class Test {
	public static Gson gson=new Gson();
	public static void main(String[] args) throws Exception {
		String tokenStr=requestGet(Consts.token);
		Token token=gson.fromJson(tokenStr, Token.class);
		String result=requestPost2(Consts.server_api,new File("16k.wav"),token,"wav");
		ReturnMsg msg=gson.fromJson(result, ReturnMsg.class);
		System.out.println(msg.getResult().get(0));
	}
	
	/**
	 * 语音识别(调用百度接口)
	 * @param urlStr
	 * @param file
	 * @param token
	 * @return
	 */
	public static String requestPost2(String urlStr,File file,Token token,String type)  throws Exception  {
		URL url=new URL(urlStr);
		int len;
		byte[] bb=new byte[1024*2];
		HttpURLConnection connect=(HttpURLConnection) url.openConnection();
		connect.setRequestMethod("POST");
        //指示应用程序要将数据写入URL连接,其值默认为false（是否传参）
		connect.setDoOutput(true);
		connect.setRequestProperty("Content-Type", "application/json");
		ByteArrayOutputStream byteOut=new ByteArrayOutputStream();	
		Map<String,Object>pm=new HashMap<String,Object>();
		pm.put("format", type);
		pm.put("rate", "16000");
		pm.put("channel", "1");
		pm.put("cuid", "34-68-95-91-77-43");
		pm.put("token", token.getAccess_token());
		pm.put("dev_pid", "1537");
		String base64Str=file2Base64(file);
		long lenFile=file.length();
		pm.put("speech",base64Str);
		pm.put("len", lenFile);
		String param=convertParam2(pm);
		byteOut.write(param.getBytes());
		//获取字节数组
		byte[]bina=byteOut.toByteArray();
		//设置数据长度
		connect.setRequestProperty("Content-Length",bina.length+"");
		OutputStream out = connect.getOutputStream();
		out.write(bina);
		out.flush();
		out.close();
		InputStream in=connect.getInputStream();
		
		String result="";
		while((len=in.read(bb))>0) {
			result+=new String(bb,0,len);
		}
		return result;
	}
	
	//获取token
	public static String requestGet(String urlStr) throws Exception {
		URL url=new URL(urlStr);
		HttpURLConnection connect=(HttpURLConnection) url.openConnection();
		InputStream in=connect.getInputStream();
		int len;
		byte[] bb=new byte[1024*2];
		String result="";
		while((len=in.read(bb))>0) {
			result+=new String(bb,0,len);
		}
		return result;
	}
	
	public static String convertParam2(Map<String,Object>param) {
		Gson gson=new Gson();
		return gson.toJson(param);
	}
	public static String file2Base64(File file) throws Exception {
		Base64 base64=new Base64();
		ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
		FileInputStream in=new FileInputStream(file);
		int len;
		byte[]bb=new byte[1024*2];
		while((len=in.read(bb))>0){
			byteOut.write(bb, 0, len);
		}
		in.close();
		byte[] bytes=byteOut.toByteArray();
		byteOut.close();
		return base64.encode(bytes);
	}
}
